package com.hzit.cart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzit.cart.api.domain.Order;
import com.hzit.cart.api.domain.OrderItem;
import com.hzit.cart.mapper.OrderMapper;
import com.hzit.cart.service.IOrderItemService;
import com.hzit.cart.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hbxfwf
 * @since 2023-09-21
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

	@Autowired
	private IOrderItemService orderItemService;

	//1. 根据登录用户查询订单列表
	@Override
	public List<Order> getOrderList(String login) {
		//1.1 根据登录用户id查询订单列表
		List<Order> orderList = this.list(new QueryWrapper<Order>().eq("user_id", login));
		//1.2 遍历orderList为其下的订单列表赋值
		for (Order order : orderList) {
			//1.2.1 根据订单id查询出订单项列表
			List<OrderItem> orderItems = orderItemService.list(new QueryWrapper<OrderItem>().eq("order_id", order.getOrderId()));
			//1.2.2 将订单项列表与当前订单绑定
			order.setOrderItemList(orderItems);
		}
		return orderList;
	}
}
